// API基础URL
const API_BASE_URL = '/api';

// 全局axios配置
axios.defaults.baseURL = API_BASE_URL;
axios.defaults.headers.common['Content-Type'] = 'application/json';

// 错误处理
axios.interceptors.response.use(
    response => response,
    error => {
        if (error.response) {
            // 服务器返回错误信息
            const errorMsg = error.response.data.error || '请求失败';
            Vue.prototype.$message.error(errorMsg);
        } else if (error.request) {
            // 请求发送但没有收到响应
            Vue.prototype.$message.error('服务器无响应，请检查网络连接');
        } else {
            // 请求设置出错
            Vue.prototype.$message.error('请求配置错误: ' + error.message);
        }
        return Promise.reject(error);
    }
);

// 等待DOM和所有脚本加载完成
document.addEventListener('DOMContentLoaded', function() {
    // 确保组件已加载
    if (typeof Dashboard === 'undefined') {
        console.error('组件未加载，请检查脚本引用');
        return;
    }

    // 路由配置
    const routes = [
        { path: '/', component: Dashboard },
        { path: '/consumers/list', component: ConsumersList },
        { path: '/consumers/create', component: ConsumersCreate },
        { path: '/consumers/edit/:id', component: ConsumersCreate, props: true },
        { path: '/routes/list', component: RoutesList },
        { path: '/routes/create', component: RoutesCreate },
        { path: '/routes/edit/:id', component: RoutesCreate, props: true },
        { path: '/upstreams/list', component: UpstreamsList },
        { path: '/upstreams/create', component: UpstreamsCreate },
        { path: '/upstreams/edit/:id', component: UpstreamsCreate, props: true },
        { path: '/services/list', component: ServicesList },
        { path: '/services/create', component: ServicesCreate },
        { path: '/services/edit/:id', component: ServicesCreate, props: true },
        { path: '/token', component: TokenGenerator }
    ];

    const router = new VueRouter({
        routes
    });

    // 创建Vue实例
    new Vue({
        el: '#app',
        router,
        data: {
            activeMenu: '/',
            isCollapse: false
        },
        created() {
            // 初始化时设置当前活动菜单
            this.activeMenu = this.$route.path;
        },
        watch: {
            // 监听路由变化，更新活动菜单
            '$route'(to) {
                this.activeMenu = to.path;
            }
        },
        methods: {
            // 切换侧边栏折叠状态
            toggleSidebar() {
                this.isCollapse = !this.isCollapse;
            }
        }
    });
}); 